
package kaizone.haining.chaobohui.fragment;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.CBHApplication;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.json.UserDO;
import kaizone.haining.chaobohui.json.VersionDO;
import kaizone.haining.chaobohui.login.FollowControl;
import kaizone.haining.chaobohui.login.UserControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.setup.APPAbout;
import kaizone.haining.chaobohui.setup.WeiXin;
import kaizone.haining.chaobohui.setup.XinLangWeiBo;
import kaizone.haining.chaobohui.widget.UsersDetail;
import kaizone.haining.chaobohui.widget.UsersFunction;
import kaizone.haining.chaobohui.widget.UsersFunction.OnCheckedChangeListener;
import kaizone.haining.chaobohui.widget.UsersFunction.OnClickListener;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CBHUsersFragment extends Fragment implements OnClickListener, OnCheckedChangeListener,
        DataControl.IDataUpdateListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/user/remote/rest/my.htm";

    private UsersDetail mDetail;

    private UsersFunction mFunction01;

    private UsersFunction mFunction02;

    private UsersFunction mFunction03;

    private DataControl mDataControl;

    private UserControl mUserControl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // if (true) {
        // prompt("即将开通");
        // return null;
        // }
        View view = (View) inflater.inflate(R.layout.cbh_fragment_users, null);
        mDetail = (UsersDetail) view.findViewById(R.id.user01);
        mFunction01 = (UsersFunction) view.findViewById(R.id.user02);
        mFunction02 = (UsersFunction) view.findViewById(R.id.user03);
        mFunction03 = (UsersFunction) view.findViewById(R.id.user04);
        initWidget();
        initControl();
        return view;
    }

    private void initWidget() {
        mFunction01.setText01("绑定新浪微博");
        mFunction01.setText02("绑定微信");
        mFunction01.setOnClickListener(this);
        mFunction01.setToggleButtonVisibility(View.GONE);

        mFunction02.setText01("消息推送");
        mFunction02.setText02("一键分享设置");
        mFunction02.setOnClickListener(this);
        mFunction02.setOnCheckedChangeListener(this);
        mFunction02.setToggleButtonVisibility(View.VISIBLE);
        mFunction02.setVisibility(View.GONE);

        mFunction03.setText01("检查新版本");
        mFunction03.setOnClickListener(this);
        mFunction03.setPanel02Visibility(View.GONE);
    }

    private void initControl() {
        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        mUserControl = new FollowControl(getActivity());
        requestMy();
    }

    public void requestMy() {
        String url = mUserControl.requestMy();
        mDataControl.exec(url);
    }

    public void requstUpateVersion() {
        String url = APPAbout.CHECK_FOR_UPDATE;
        mDataControl.exec(url);
    }

    public void bindXinLanweibo() {
        CBHApplication app = (CBHApplication) getActivity().getApplication();
        XinLangWeiBo weibo = app.getXinLangWeiBo();
        if (weibo == null) {
            weibo = new XinLangWeiBo(getActivity());
            app.setXinLangWeibo(weibo);
        }//
        if (!weibo.isSessionValid()) {
            weibo.authorize(getActivity());
        } else {
            Toast.makeText(getActivity(), "微博已绑定", 500).show();
        }
    }

    private void bindWeiXin() {
        CBHApplication app = (CBHApplication) getActivity().getApplication();
        WeiXin weixin = app.getWeiXin();
        if (weixin == null) {
            weixin = new WeiXin(getActivity());
            app.setWeiXin(weixin);
        }

        if (!weixin.isWXAppInstalled()) {
            Toast.makeText(getActivity(), "未安装微信", 300).show();
            return;
        }

        if (!weixin.isWXAppSupportAPI()) {
            Toast.makeText(getActivity(), "微信版本过低", 300).show();
            return;
        }

        Toast.makeText(getActivity(), "微信已绑定", 500).show();
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (requeststring.equals(mUserControl.requestMy())) {
            UserDO userDO = UserDO.parseJson(datastring);
            return userDO;
        }//
        else if (requeststring.equals(APPAbout.CHECK_FOR_UPDATE)) {
            VersionDO versionDO = VersionDO.parseJson(datastring);
            return versionDO;
        }
        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (requeststring.equals(mUserControl.requestMy())) {
            if (result instanceof UserDO) {
                UserDO userDO = (UserDO) result;
                if (!userDO.success)
                    return;
                mDetail.setData(userDO.getData());
            }
        }//
        else if (requeststring.equals(APPAbout.CHECK_FOR_UPDATE)) {
            if (result instanceof VersionDO) {
                VersionDO versiondo = (VersionDO) result;
                final APPAbout about = new APPAbout(getActivity());
                about.check(versiondo);
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        // 绑定新浪微博
        if (v == mFunction01.getTextView01()) {
            bindXinLanweibo();
        }
        // 绑定QQ空间
        else if (v == mFunction01.getTextView02()) {
            bindWeiXin();
        }
        // 消息推送
        else if (v == mFunction02.getTextView01()) {

        }
        // 一键分享设置
        else if (v == mFunction02.getTextView02()) {

        }
        // 检查新版本
        else if (v == mFunction03.getTextView01()) {
            requstUpateVersion();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub

    }

    public static CBHUsersFragment newInstance() {
        final CBHUsersFragment f = new CBHUsersFragment();
        return f;
    }

    public void prompt(String msg) {
        new AlertDialog.Builder(getActivity()).setMessage(msg).setPositiveButton("确定", null).show();
    }

}
